package com.baizhi.service.impl;

import com.baizhi.entity.User;
import com.baizhi.mapper.UserMapper;
import com.baizhi.service.UserService;
import com.baizhi.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVo<User> showAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectAllUserAndOneComment();
        PageInfo<User>info=new PageInfo<>(users);

        UserVo<User> userVo = new UserVo<>();

        Map<Integer, List<User>> map = userVo.getMap();

        for (User user : users) {
            Integer userId = user.getId();
            List<User> userList = userMapper.selectUserAndTwoComment(userId);
            map.put(userId,userList);
        }

        userVo.setMap(map);
        userVo.setInfo(info);
        return userVo;
    }
}
